package com.xiaozhichao.databafflesys.repository;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author:xiaozhichao
 * @date:2021/3/22
 * 代码生成器
 */
public class CodeGenerator {

    private static String authorName = "xiaozhichao";
    private static String projectPath = System.getProperty("user.dir") + "/data-baffle-sys-repository";
    private static String[] tables = new String[]{
            "baffle_app", "baffle_datasource", "baffle_env", "baffle_interface", "baffle_message","baffle_config","baffle_fail_tmp","baffle_user"
    };

    public static void main(String[] args) {
        int result = 0;
        AutoGenerator mpg = new AutoGenerator().setGlobalConfig(
                new GlobalConfig()
                        .setOutputDir(projectPath + "/src/main/java")
                        .setFileOverride(true)
                        .setActiveRecord(true)
                        .setEnableCache(false)
                        .setBaseResultMap(true)
                        .setBaseColumnList(true)
                        .setOpen(true)
                        .setAuthor(authorName)
                        .setMapperName("%sMapper")
                        .setXmlName("%sMapper")
                        .setServiceName("%sService")
                        .setServiceImplName("%sServiceImpl")
        ).setDataSource(
                new DataSourceConfig()
                        .setDbType(DbType.MYSQL)
                        .setDriverName("com.mysql.jdbc.Driver")
                        .setUsername("root")
                        .setPassword("root")
                        .setUrl("jdbc:mysql://127.0.0.1:3306/data-baffle")
        ).setStrategy(
                new StrategyConfig()
                        .setNaming(NamingStrategy.underline_to_camel)
                        .setInclude(tables)

        ).setPackageInfo(
                new PackageConfig()
                        .setParent("com.xiaozhichao.databafflesys.repository")
                        .setEntity("model")
                        .setMapper("mapper")
                        .setService("service")
                        .setServiceImpl("service.impl")
        ).setCfg(
                new InjectionConfig() {
                    @Override
                    public void initMap() {
                        Map<String,Object> map = new HashMap<>();
                        map.put("abc",this.getConfig().getGlobalConfig() + "-mp");
                        this.setMap(map);
                    }
                }.setFileOutConfigList(Collections.singletonList(new FileOutConfig("/templates/mapper.xml.vm") {
                    @Override
                    public String outputFile(TableInfo tableInfo) {
                        return projectPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper.xml";
                    }
                }))
        ).setTemplate(
                new TemplateConfig().setXml(null)
        );
        if(1 == result){
            mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        }
        mpg.execute();
    }
}
